package net.borderwars.simulator;

import net.borderwars.map.QuadTree;
import net.borderwars.simulator.events.BEvent;

import java.io.*;
import java.util.logging.Logger;

/**
 * @author Eric
 *         Date: Feb 7, 2004
 *         Time: 9:48:10 PM
 */
public class BattlePlane implements Serializable {
    private static Logger log = Logger.getLogger ("net.borderwars.simulator.BattlePlane");
    private EventQueue eq = new EventQueue ();
    private EventProcessor ep = new EventProcessor (eq);

    private int width = 4096;
    private int height = 4096;

    public QuadTree map = new QuadTree (this, width, height);  // TODO: this shouldn't be public    nor static
    private static final long serialVersionUID = -6739933877968374390L;

    public BattlePlane () {
        ep.start ();
    }

    public int getWidth () {
        return (width);
    }

    public int getHeight () {
        return (height);
    }

    public static BattlePlane findOrCreate () {
        log.entering ("net.borderwars.simulator.BattlePlane", "findOrCreate");
        File f = new File ("battleplane.dat");
        BattlePlane rtn = null;
        if (f.exists ()) {
            try {
                BufferedInputStream bis = new BufferedInputStream (new FileInputStream (f));
                ObjectInputStream ois = new ObjectInputStream (bis);
                rtn = (BattlePlane) ois.readObject ();
                return (rtn);
            }
            catch (FileNotFoundException e) {
                e.printStackTrace ();
            }
            catch (IOException e) {
                e.printStackTrace ();
            }
            catch (ClassNotFoundException e) {
                e.printStackTrace ();
            }
        }
        rtn = new BattlePlane ();
        log.exiting ("net.borderwars.simulator.BattlePlane", "findOrCreate");
        return (rtn);
    }

    public Clock getClock () {
        return (eq.getClock ());

    }

    public class DumbEvent extends BEvent {

        public DumbEvent (long l) {
            super (l);
        }

        public void process () {
            DumbEvent de = new DumbEvent (eq.getClock ().getCurrentTime () + 100);
            eq.placeEvent (de);
            Thread.yield ();
        }

        public String getName () {
            return ("Dumb Event for testing");
        }
    }

    public EventQueue getEventQueue () {
        return (eq);
    }
}

